import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:冒泡
 * User: dings
 * Date: 2024-07-29
 * Time: 22:28
 */
public class Test {
    public void bubbleSort(int[] arr) {
        boolean flg = false;
        for (int i = 0; i < arr.length-1; i++) {
            for (int j = 0; j < arr.length-1-i; j++) {
                if (arr[j] > arr[j+1]) {
                    swap(arr,j,j+1);
                    flg = true;
                }
            }
            if (!flg) {
                return;
            }
        }
    }
    public void swap(int[] arr,int i,int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

    public static void main(String[] args) {
        int[] arr = {3,45,2,56,0,32,23,12,6};
        Test test = new Test();
        test.bubbleSort(arr);
        System.out.println(Arrays.toString(arr));
    }
}
